package com.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 电脑基类
class Computer {
    private String name;
    private String brand;
    private String cpu;
    private String memory;
    private String hardDisk;
    private String monitor;

    public Computer(String name, String brand, String cpu, String memory, String hardDisk, String monitor) {
        this.name = name;
        this.brand = brand;
        this.cpu = cpu;
        this.memory = memory;
        this.hardDisk = hardDisk;
        this.monitor = monitor;
    }

    // 显示电脑信息的方法，子类可以重写
    public void displayInfo() {
        System.out.println("电脑名称: " + name);
        System.out.println("品牌: " + brand);
        System.out.println("CPU: " + cpu);
        System.out.println("内存: " + memory);
        System.out.println("硬盘: " + hardDisk);
        System.out.println("显示器: " + monitor);
    }

    // getter方法
    public String getName() {
        return name;
    }
}

// 笔记本类
class NoteBook extends Computer {
    private String battery; // 9芯或6芯

    public NoteBook(String name, String brand, String cpu, String memory, String hardDisk, String monitor, String battery) {
        super(name, brand, cpu, memory, hardDisk, monitor);
        this.battery = battery;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("电池: " + battery + "芯");
        System.out.println("类型: 笔记本");
    }
}

// 台式机类
class Desktop extends Computer {
    private String hostType; // 卧式或立式

    public Desktop(String name, String brand, String cpu, String memory, String hardDisk, String monitor, String hostType) {
        super(name, brand, cpu, memory, hardDisk, monitor);
        this.hostType = hostType;
    }

    @Override
    public void displayInfo() {
        super.displayInfo();
        System.out.println("机箱类型: " + hostType);
        System.out.println("类型: 台式机");
    }
}

// 电脑管理系统
public class ComputerManagementSystem {
    private List<Computer> computers = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        ComputerManagementSystem system = new ComputerManagementSystem();
        system.run();
    }

    public void run() {
        while (true) {
            System.out.println("\n请选择功能:");
            System.out.println("1. 查看电脑信息");
            System.out.println("2. 增加电脑信息");
            System.out.println("3. 删除电脑信息");
            System.out.println("4. 退出");
            System.out.print("请输入选项(1-4): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    displayAllComputers();
                    break;
                case 2:
                    addComputer();
                    break;
                case 3:
                    deleteComputer();
                    break;
                case 4:
                    System.out.println("系统退出，谢谢使用!");
                    return;
                default:
                    System.out.println("无效选项，请重新输入!");
            }
        }
    }

    // 显示所有电脑信息
    private void displayAllComputers() {
        if (computers.isEmpty()) {
            System.out.println("当前没有电脑信息!");
            return;
        }

        System.out.println("\n电脑信息列表:");
        for (int i = 0; i < computers.size(); i++) {
            System.out.println("序号: " + (i + 1));
            computers.get(i).displayInfo();
            System.out.println("---------------------");
        }
    }

    // 添加电脑
    private void addComputer() {
        System.out.println("\n请选择要添加的电脑类型:");
        System.out.println("1. 笔记本");
        System.out.println("2. 台式机");
        System.out.print("请输入选项(1-2): ");
        
        int type = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        System.out.print("请输入电脑名称: ");
        String name = scanner.nextLine();
        System.out.print("请输入品牌: ");
        String brand = scanner.nextLine();
        System.out.print("请输入CPU: ");
        String cpu = scanner.nextLine();
        System.out.print("请输入内存: ");
        String memory = scanner.nextLine();
        System.out.print("请输入硬盘: ");
        String hardDisk = scanner.nextLine();
        System.out.print("请输入显示器: ");
        String monitor = scanner.nextLine();

        if (type == 1) {
            System.out.print("请输入电池类型(9芯或6芯): ");
            String battery = scanner.nextLine();
            computers.add(new NoteBook(name, brand, cpu, memory, hardDisk, monitor, battery));
        } else if (type == 2) {
            System.out.print("请输入机箱类型(卧式或立式): ");
            String hostType = scanner.nextLine();
            computers.add(new Desktop(name, brand, cpu, memory, hardDisk, monitor, hostType));
        } else {
            System.out.println("无效的电脑类型!");
            return;
        }

        System.out.println("添加成功! 当前电脑列表:");
        displayAllComputers();
    }

    // 删除电脑
    private void deleteComputer() {
        if (computers.isEmpty()) {
            System.out.println("当前没有电脑信息可删除!");
            return;
        }

        displayAllComputers();
        System.out.print("请输入要删除的电脑序号: ");
        int index = scanner.nextInt() - 1;
        scanner.nextLine(); // 消耗换行符

        if (index >= 0 && index < computers.size()) {
            String name = computers.get(index).getName();
            computers.remove(index);
            System.out.println("已成功删除电脑: " + name);
            System.out.println("删除后电脑列表:");
            displayAllComputers();
        } else {
            System.out.println("无效的序号!");
        }
    }
}
